unit uWyborKTM;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, GridsEh, DBGridEh, ExtCtrls;

type
  TFWyborKTM = class(TForm)
    DBGridEh1: TDBGridEh;
    qKTM: TADOQuery;
    dsKTM: TDataSource;
    edKTM: TEdit;
    edNazwa: TEdit;
    btAddOne: TButton;
    btDelOne: TButton;
    btDelAll: TButton;
    lbKTM: TListBox;
    Bevel1: TBevel;
    Button2: TButton;
    Button1: TButton;
    procedure btAddOneClick(Sender: TObject);
    procedure btDelAllClick(Sender: TObject);
    procedure btDelOneClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure edChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    KTMy : string;
  end;

var
  FWyborKTM: TFWyborKTM;

implementation

{$R *.dfm}

procedure TFWyborKTM.btAddOneClick(Sender: TObject);
var
  s : string;
begin
  if not qKTM.IsEmpty then begin
    s := trim(qKTM.FieldByName('ITEMNMBR').AsString);
    if lbKTM.Items.IndexOf(s)<0 then
      lbKTM.Items.Add(s);
  end;
end;

procedure TFWyborKTM.btDelAllClick(Sender: TObject);
begin
  lbKTM.Items.Clear;
end;

procedure TFWyborKTM.btDelOneClick(Sender: TObject);
begin
  if lbKTM.ItemIndex>=0 then begin
    lbKTM.Items.Delete(lbKTM.ItemIndex);
  end;
end;

procedure TFWyborKTM.Button1Click(Sender: TObject);
begin
  lbKTM.Clear;
  Close;
end;

procedure TFWyborKTM.Button2Click(Sender: TObject);
var
  i : integer;
begin
  KTMy := '';
  for i  := 0 to lbKTM.Items.Count - 1 do
    KTMy := KTMy + lbKTM.Items[i]+',';
  KTMy := copy(KTMy, 1, length(KTMy)-1);
  Close;
end;

procedure TFWyborKTM.edChange(Sender: TObject);
var
  s : string;
begin
  qKTM.Filtered := False;
  s := '';
  if trim(edKTM.Text)<> '' then begin
    s := 'ITEMNMBR like ''%'+trim(edKTM.Text)+'%'' ';
    if trim(edNazwa.Text)<>'' then
      s := s + ' and ITEMDESC like ''%'+trim(edNazwa.Text)+'%'' ';
  end else if trim(edNazwa.Text)<>'' then begin
    s := 'ITEMDESC like ''%'+trim(edNazwa.Text)+'%'' ';
  end;
  if s<>'' then begin
    qKTM.Filter := s;
    qKTM.Filtered := True;
  end;
end;

procedure TFWyborKTM.FormShow(Sender: TObject);
begin
  qKTM.Open;
end;

end.
